package com.springboot.love.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.dao.NoticeDao;
import com.springboot.love.entity.Notice;
import com.springboot.love.service.NoticeService;
import com.springboot.love.utils.PageUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Notice)表服务实现类
 */
@Service("noticeService")
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeDao noticeDao;


    /**
     * 查询多条数据
     * @return 对象列表
     */
    @Override
    public List<Notice> queryAllByLimit(PageUtil pageUtil) {
        return this.noticeDao.queryAllByLimit(pageUtil);
    }

    @Override
    public int queryTotalByTime() {
        return noticeDao.queryTotalByTime();
    }

    /**
     * 新增数据
     *
     */

    @Override
    public String insertNotice(String noticeTitle, String noticeContent) {
        Notice notice = new Notice();
        notice.setTitle(noticeTitle);
        notice.setNoticeContent(noticeContent);
        notice.setNoticeVisitedCount(0);
        notice.setNoticeCreateTime(new Date());
        int insert = noticeDao.insert(notice);
        JSONObject json = new JSONObject();
        if (insert>0){
            json.put("success",true);
            json.put("status", 200);
        }else {
            json.put("success",false);
            json.put("status", 200);
        }
        return json.toString();
    }
    /**
     * 修改数据
     */
    @Override
    public String updateNotice(String noticeId, String noticeTitle, String noticeContent) {
        Notice notice = new Notice();
        notice.setNoticeId(Integer.parseInt(noticeId));
        notice.setTitle(noticeTitle);
        notice.setNoticeContent(noticeContent);
        notice.setNoticeVisitedCount(0);
        notice.setNoticeCreateTime(new Date());
        int update = noticeDao.updateById(notice);
        JSONObject json = new JSONObject();
        if (update>0){
            json.put("success",true);
            json.put("status", 200);
        }else {
            json.put("success",false);
            json.put("status", 200);
        }
        return json.toString();
    }

    /**
     * 单个添加公告
     * @param noticeId 主键
     * @return
     */
    @Override
    public String deleteByIdNotice(String noticeId) {
        int deleteById = noticeDao.deleteById(noticeId);
        JSONObject json = new JSONObject();
        if (deleteById>0){
            json.put("success",true);
            json.put("status", 200);
        }else {
            json.put("success",false);
            json.put("status", 200);
        }
        return json.toString();
    }

    /**
     * 批量删除公告
     * @param delete
     * @return
     */
    @Override
    public String deletesNotice(String delete) {
        List<Notice> list2=(List<Notice>) JSONArray.toList(JSONArray.fromObject(delete), Notice.class);
        List<Integer> noticeList = new ArrayList<>();
        for (Notice notice : list2){
            noticeList.add(notice.getNoticeId());
        }
        int deletesNotice = noticeDao.deleteBatchIds(noticeList);
        JSONObject json = new JSONObject();
        if (deletesNotice>0) {
            json.put("success",true);
            json.put("status", 200);
        }else {
            json.put("success",false);
            json.put("status", 200);
        }
        return json.toString();
    }
}